package com.ava.avamedia;

import java.util.ArrayList;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.ava.avamedia.account.AccountActivity;
import com.ava.avamedia.adapter.IconAdapter;
import com.ava.avamedia.lottery.RegionActivity;
import com.ava.avamedia.model.Icon;
import com.ava.avamedia.util.Constants;

public class ListIconActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_icon);
		registerReceiver(LOGOUT_Reciver, new IntentFilter(
				Constants.FINSIH_ACTION));
		GridView gridView = (GridView) findViewById(R.id.activity_list_icon_grid_view);
		ArrayList<Icon> listIcon = new ArrayList<Icon>();
		listIcon.add(new Icon(R.drawable.ico_xoso, "Xổ Số"));
		listIcon.add(new Icon(R.drawable.ic_launcher, "Thông tin"));
		IconAdapter adapter = new IconAdapter(listIcon, this);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (arg2 == 0) {
					Intent intent = new Intent();
					intent.setClass(ListIconActivity.this, RegionActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
							| Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
				}
				if (arg2 == 1) {
					Intent intent = new Intent();
					intent.setClass(ListIconActivity.this,
							AccountActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
							| Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
				}

			}
		});
	}

	private BroadcastReceiver LOGOUT_Reciver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context arg0, Intent arg1) {
			String action_finish = arg1.getStringExtra(Constants.FINISH);

			if (action_finish.equalsIgnoreCase(Constants.ACTION_LOGOUT)) {
				finish();
				// this line unregister the receiver,so that i run only one time
				// and when next time logout is clicked then again it called
			}
		}
	};
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(LOGOUT_Reciver);
	};
}
